/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2012 www.pretty-tools.com
 */

/**
 * Subscription
 */
function wsshSubscription(name)
{
    this.name = !name ? null : name;
    this.url = null;
    this.lastModified = null;
    this.updateTimeStamp = 0;
}

/**
 * Profile factory/loader
 */
var wsshSubscriptionSerializer = {
    logger: yrprxFactory.createLogger("wsshSubscriptionSerializer")
};

wsshSubscriptionSerializer.handlerSubscription = function(subscription, items)
{
    var item = new wsshSubscription();

    subscription.loadAttributes(item, ["name", "url", "lastModified", "updateTimeStamp"]);

    if (wsshWebProfileSerializer.getProfile(item.name).exists())
        item.updateTimeStamp = yrprxUtil.convertStringToTime(item.updateTimeStamp);
    else
    {
        item.lastModified = null;
        item.updateTimeStamp = 0;
    }

    items.push(item);
}

wsshSubscriptionSerializer.load = function(source)
{
    this.logger.debug("load");
    try
    {
        if (!source) source = this.getProfile();

        if (yrprxComponentsFactory.implementsIFile(source) && !source.exists())
            return new Array();

        var xml = new yrprxSimpleXMLParser(source);

        var collection = xml.getElementsByTagName("subscriptions");
        if (!collection || collection.length != 1)
            return new Array();

        var items = new Array();

        xml.enumerateElementsByTagName("subscription", wsshSubscriptionSerializer.handlerSubscription, items);
        if (this.logger.enabled) this.logger.debug("load: loaded subscriptions: " + items.length);

        return items;
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("load: Exception: " + ex);
    }
    return new Array();
}

wsshSubscriptionSerializer.save = function(items, source)
{
    this.logger.debug("save");
    try
    {
        if (!source) source = this.getProfile();

        var builder = new yrprxSimpleXMLBuilder("subscriptions");

        var savedCount = 0;
        for (var i = 0; i < items.length; ++i)
        {
            if (!items[i])
                continue;

            var subscriptionElement = builder.addNode("subscription");
            subscriptionElement.saveAttributes(items[i], ["name", "url", "lastModified"]);

            subscriptionElement.addAttribute("updateTimeStamp", yrprxUtil.convertTimeToString(items[i].updateTimeStamp));

            ++savedCount;
        }

        if (savedCount > 0)
            builder.save(source);
        else if (source.exists())
            source.remove(false);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("save: Exception: " + ex);
    }
}

wsshSubscriptionSerializer.getProfile = function()
{
    var profile = yrprxUtil.getProfileDirectory();

    profile.append("WebSlideShow");

    try
    {
        if (!profile.exists())
            profile.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 00400|00200|00100);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("getProfile: Exception: " + ex);
        // reset profile directory to root
        profile = yrprxUtil.getProfileDirectory();
    }

    profile.append("Subscriptions.xml");

    return profile;
}
